/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.put.dcs_modeler.visualModelEditor.propertiesEditor.propertyEditorsForClasses;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyEditor;


import java.beans.PropertyEditorSupport;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.KeyStroke;

import org.netbeans.api.project.Project;
import org.openide.explorer.propertysheet.ExPropertyEditor;
import org.openide.explorer.propertysheet.InplaceEditor;
import org.openide.explorer.propertysheet.PropertyEnv;
import org.openide.explorer.propertysheet.PropertyModel;
import pl.put.dcs_modeler.visualModelEditor.propertiesEditor.propertyEditorsForClasses.classPathPropertyEditor.ClassChooser;

/**
 *
 * @author LukaszR
 */
public class ClassPathPropertyEditor extends PropertyEditorSupport implements ExPropertyEditor, InplaceEditor.Factory
{

    public static Logger log = Logger.getLogger( ClassPathPropertyEditor.class.getName() );
    //private Class selectedClass;
    private String selectedClass = "";
    private InplaceEditor ed = null;
    private static ClassChooser classChooser; // static, bo moze byc uruchomiony jedna kopia wybieracza klasy
    private Project project;


    public ClassPathPropertyEditor()
    {
        super();

        log.info( "kopia PropertyEditorSupport");
    }

    public Project getProject()
    {
        return project;
    }

    public void setProject( Project project )
    {
        this.project = project;
        log.info( "setProject ----");
        //classChooser = new ClassChooser( project );
    }



    @Override
    public String getAsText()
    {
        if ( classChooser != null )
        {
            log.info( "get as text: " + classChooser.getSelectedClass() );
            //setValue( log );
            return classChooser.getSelectedClass();//classChooser.getSelectedClass();//.getPackage().getName() + selectedClass.getName();
        }
        return null;
    }

    public void setAsText( Class c )
    {
        
        //setValue( "jeden" );
        
    }

    @Override
    public void setAsText( String s )
    {
        log.info( "set as Text");
        log.info( s );
        //classChooser.setSelectedClass( s );
    }

    public String getSelectedClass()
    {
        return selectedClass;
    }

    public void setSelectedClass( String selectedClass )
    {
        this.selectedClass = selectedClass;
        setValue( selectedClass );
    }


    @Override
    public Component getCustomEditor()
    {
        log.info( "project test: " + project.getProjectDirectory().getPath() );
        classChooser = new ClassChooser( project );
        classChooser.addPropertyChangeListener( "selectedClass", new PropertyChangeListener()
                        {

                            @Override
                            public void propertyChange( PropertyChangeEvent evt )
                            {
                                setSelectedClass( ( String )evt.getNewValue() );
                                log.info( "dupe : " + selectedClass );
                                firePropertyChange();
                            }
                        });
                        log.info( "polik00:" + selectedClass );

        classChooser.setSelectedClass( ( String )this.getValue() );
        return classChooser;
    }

    @Override
    public Object getValue()
    {
        log.info( "getValue:" + super.getValue() );
        return super.getValue();
    }

    @Override
    public void setValue( Object val )
    {
        log.info("set value");
        if ( classChooser != null )
        {
            classChooser.setSelectedClass( (String)val );
        }
        super.setValue( val );
    }


    @Override
    public boolean supportsCustomEditor()
    {
        return true;
    }

    @Override
    public void attachEnv( PropertyEnv env )
    {
        env.registerInplaceEditorFactory( this );
        log.info( "env!!!");
        project = ( Project )env.getFeatureDescriptor().getValue( "project" );
        //classChooser.setProject( project );
    }  

    @Override
    public InplaceEditor getInplaceEditor()
    {
        
        return null;
    }

    @Override
    public boolean isPaintable()
    {
        return false;
    }

    private class Inplace implements InplaceEditor 
    {
        private PropertyEditor editor;
         //private final JX picker = new JXDatePicker();

        //ClassChooser classChooser = new ClassChooser();
        JTree picker = new JTree();
        private PropertyModel model;


        @Override
        public void connect( PropertyEditor propertyEditor, PropertyEnv env )
        {
            editor = propertyEditor;
            reset();

        }

        @Override
        public JComponent getComponent()
        {
            return picker;
        }

        @Override
        public void clear()
        {
            editor = null;
            // classChooser = null;
            model = null;
        }

        @Override
        public Object getValue()
        {
            return classChooser.getSelectedClass();
        }

        @Override
        public void setValue( Object o )
        {
            
        }

        @Override
        public boolean supportsTextEntry()
        {
            return false;
        }

        @Override
        public void reset()
        {
            
        }

        @Override
        public void addActionListener( ActionListener al )
        {
            
        }

        @Override
        public void removeActionListener( ActionListener al )
        {
            
        }

        @Override
        public KeyStroke[] getKeyStrokes()
        {
            return new KeyStroke[0];
        }

        @Override
        public PropertyEditor getPropertyEditor()
        {
            return editor;
        }

        @Override
        public PropertyModel getPropertyModel()
        {
            return model;
        }

        @Override
        public void setPropertyModel( PropertyModel pm )
        {
            model = pm;
        }

        @Override
        public boolean isKnownComponent( Component c )
        {
            return c == classChooser || classChooser.isAncestorOf( c );
        }

    }

}
